package com.kaizzi.data.jdl.helper;

import com.kaizzi.data.common.been.TempBeen;
import com.kaizzi.data.common.utils.DateUtil;
import com.kaizzi.data.common.utils.JsonUtil;
import com.kaizzi.data.common.utils.LogicUtil;
import com.kaizzi.data.jdl.model.ExcelModel;
import com.kaizzi.data.jdl.model.Page32Model;
import lombok.Data;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;

import static java.util.stream.Collectors.toList;

/**
 * @author Kzen
 * @Description description
 * @date 2020/10/15
 */
@Component
public class PageData32 {

    public String getData(ExcelModel model){
        List<Page32Model> page32s = model.getPage32s();
        List<Integer> list = new ArrayList<>();
        for (int i = 0; i < page32s.size(); i++) {
            if (page32s.get(i).getCount().equals("人数")){
                list.add(i);
            }
        }
        List<Page32Model> genderList = page32s.stream().skip(0).limit(3).collect(toList());
        List<Page32Model> ageList = page32s.stream().skip(list.get(0)).limit(list.get(1) - list.get(0)).collect(toList());
        List<Page32Model> areaList = page32s.stream().skip(list.get(1)).collect(toList());

        Page32 page32 = new Page32();

        List<TempBeen> genders = getTempBeen(genderList);
        List<TempBeen> ages = getTempBeen(ageList);
        List<TempBeen> areas = getTempBeen(areaList);
        genders.sort(Comparator.comparing(TempBeen::getTempCount));
        ages.sort(Comparator.comparing(TempBeen::getTempCount).reversed());
        areas.sort(Comparator.comparing(TempBeen::getTempCount).reversed());

        page32.setTitle(LogicUtil.getTitle(model,32));
        page32.setDataPeriod(DateUtil.getLastWeekDate());
        page32.setGenderTitle(getGenderTitle(genders));
        page32.setAgeTitle(getAgeTitle(ages));
        page32.setAreaTitle(getAreaTitle(areas));
        page32.setGenders(genders);
        page32.setAges(ages);
        page32.setAreas(areas);

        return JsonUtil.serialize(page32);
    }

    private String getGenderTitle(List<TempBeen> genders) {
        long man = 0;
        long sum = 0;
        for (TempBeen gender : genders) {
            if (gender.getTempName().equals("男")) {
                man = gender.getTempCount();
            }
            sum += gender.getTempCount();
        }

        int manRate = (int) ((double) man / (double) sum * 10);
        int womanRate = 10 - manRate;

        return "男女比例在" + manRate + ":" + womanRate + "左右";
    }

    private String getAgeTitle(List<TempBeen> ages){
        return "公众号粉丝年龄以"+ages.get(0).getTempName()+"、"+ages.get(1).getTempName()+"为主";
    }

    private String getAreaTitle(List<TempBeen> areas){
        return "粉丝地域分布以"+areas.get(0).getTempName()+"为主";
    }

    private List<TempBeen> getTempBeen(List<Page32Model> list){
        List<TempBeen> beens = new ArrayList<>();
        for (Page32Model pageModel : list) {
            if (pageModel.getCount().equals("人数")){
                continue;
            }
            TempBeen tempBeen = new TempBeen();
            tempBeen.setTempName(pageModel.getName());
            tempBeen.setTempCount(Long.parseLong(pageModel.getCount().replace(",","")));
            beens.add(tempBeen);
        }
        return beens;
    }

    @Data
    private static class Page32{
        private String title;
        private String dataPeriod;
        private String genderTitle;
        private String ageTitle;
        private String areaTitle;
        private List<TempBeen> genders;
        private List<TempBeen> ages;
        private List<TempBeen> areas;
    }
}
